import { notification } from 'antd';
import { history } from 'umi';

const requestErrorHandler = (err: {
  response: { status: number };
  data: { message: any };
}) => {
  const codeMap = {
    '021': '发生错误啦',
    '022': '发生大大大大错误啦',
    '422': '似乎是我自定义的错误啦',
  };
  if (err.response) {
    if (err.response.status === 422) {
      notification.error({
        message: '似乎遇到了什么错误,即将跳转到登录页',
        description: err.data.message,
      });
      setTimeout(() => {
        history.push('/login');
      }, 1000 * 2);
      return;
    }
    notification.error({
      message: '似乎遇到了什么错误',
      // @ts-ignore
      description: codeMap[err.response.status],
    });
    return;
  }
};

export default requestErrorHandler;
